package com.jyxd.web.task;

import com.jyxd.web.data.basic.MedOrderBackupsExec;
import com.jyxd.web.data.basic.MedOrderExec;
import com.jyxd.web.data.basic.QuartzTask;
import com.jyxd.web.data.basic.QuartzTime;
import com.jyxd.web.service.assessment.basic.MedOrderBackupsExecService;
import com.jyxd.web.service.assessment.basic.MedOrderExecService;
import com.jyxd.web.service.assessment.basic.QuartzTaskService;
import com.jyxd.web.util.SpringUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.DateUtil.getLaterMonthDate;
import static com.jyxd.web.util.UUIDUtil.getUUID;

public class MedOrderBackupsJob implements Job {

    private static Logger logger = LoggerFactory.getLogger(MedOrderBackupsJob.class);

    //定义一个成员变量
    private static QuartzTask quartzTask;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            logger.info("开始对医嘱数据进行分表备份");
            QuartzTime quartzTime = new QuartzTime();
            quartzTime.setStartTime(new Date());

            ServletContext context = null;
            //获取service
            context = (ServletContext) jobExecutionContext.getScheduler().getContext()
                    .get(QuartzServletContextListener.MY_CONTEXT_NAME);
            WebApplicationContext cxt = (WebApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            QuartzTaskService quartzTaskService = (QuartzTaskService) cxt.getBean("quartzTaskService");
            if (quartzTask == null) {
                //为空时 表明第一次执行该方法 需要查询赋值
                Map<String, Object> map = new HashMap<>();
                map.put("jobName", "MedOrderBackupsJob");
                map.put("jobGroup", "MedOrderBackupsJob");
                quartzTask = quartzTaskService.queryDataByNameAndGroup(map);
            }
            MedOrderExecService medOrderExecService = SpringUtil.getBean(MedOrderExecService.class);
            MedOrderBackupsExecService medOrderBackupsExecService = SpringUtil.getBean(MedOrderBackupsExecService.class);
            Date laterMonthDate = getLaterMonthDate(new Date(), -3l);
            Map<String, Object> map = new HashMap<>();
            map.put("startTime", laterMonthDate);
            List<MedOrderExec> medOrderExecList = medOrderExecService.queryByCreateTime(map);
            if (CollectionUtils.isEmpty(medOrderExecList))
                return;
            medOrderExecList.forEach(medOrderExec -> {
                MedOrderBackupsExec medOrderBackupsExec = new MedOrderBackupsExec();
                medOrderBackupsExec.setId(getUUID());
                medOrderBackupsExec.setVisitId(medOrderExec.getVisitId());
                medOrderBackupsExec.setVisitCode(medOrderExec.getVisitCode());
                medOrderBackupsExec.setPatientId(medOrderExec.getPatientId());
                medOrderBackupsExec.setOrderCode(medOrderExec.getOrderCode());
                medOrderBackupsExec.setOrderNo(medOrderExec.getOrderNo());
                medOrderBackupsExec.setOrderSubNo(medOrderExec.getOrderSubNo());
                medOrderBackupsExec.setOrderName(medOrderExec.getOrderName());
                medOrderBackupsExec.setSpecs(medOrderExec.getSpecs());
                medOrderBackupsExec.setDrugType(medOrderExec.getDrugType());
                medOrderBackupsExec.setOrderAttr(medOrderExec.getOrderAttr());
                medOrderBackupsExec.setDefaultTimePoint(medOrderExec.getDefaultTimePoint());
                medOrderBackupsExec.setExecuteDate(medOrderExec.getExecuteDate());
                medOrderBackupsExec.setExplicitTimeInterval(medOrderExec.getExplicitTimeInterval());
                medOrderBackupsExec.setDuration(medOrderExec.getDuration());
                medOrderBackupsExec.setStartTimePoint(medOrderExec.getStartTimePoint());
                medOrderBackupsExec.setCompleteTimePoint(medOrderExec.getCompleteTimePoint());
                medOrderBackupsExec.setEntryTime(medOrderExec.getEntryTime());
                medOrderBackupsExec.setDosage(medOrderExec.getDosage());
                medOrderBackupsExec.setQuantity(medOrderExec.getQuantity());
                medOrderBackupsExec.setQuantityUnits(medOrderExec.getQuantityUnits());
                medOrderBackupsExec.setDosageUnits(medOrderExec.getDosageUnits());
                medOrderBackupsExec.setAllDosage(medOrderExec.getAllDosage());
                medOrderBackupsExec.setTotalNumber(medOrderExec.getTotalNumber());
                medOrderBackupsExec.setRepeatIndicator(medOrderExec.getRepeatIndicator());
                medOrderBackupsExec.setUseMode(medOrderExec.getUseMode());
                medOrderBackupsExec.setClassType(medOrderExec.getClassType());
                medOrderBackupsExec.setFrequency(medOrderExec.getFrequency());
                medOrderBackupsExec.setFrequencyEndTime(medOrderExec.getFrequencyEndTime());
                medOrderBackupsExec.setPerformSpeed(medOrderExec.getPerformSpeed());
                medOrderBackupsExec.setSpeedUnits(medOrderExec.getSpeedUnits());
                medOrderBackupsExec.setNurseCode(medOrderExec.getNurseCode());
                medOrderBackupsExec.setOrderStatus(medOrderExec.getOrderStatus());
                medOrderBackupsExec.setRemark(medOrderExec.getRemark());
                medOrderBackupsExec.setDoctorAdvice(medOrderExec.getDoctorAdvice());
                medOrderBackupsExec.setCreateTime(medOrderExec.getCreateTime());
                medOrderBackupsExec.setUpdateTime(medOrderExec.getUpdateTime());
                medOrderBackupsExec.setIsSync(medOrderExec.getIsSync());
                medOrderBackupsExec.setOrderExecNum(medOrderExec.getOrderExecNum());
                medOrderBackupsExec.setSyncNum(medOrderExec.getSyncNum());
                medOrderBackupsExec.setRecentSyncTime(medOrderExec.getRecentSyncTime());
                medOrderBackupsExec.setIdm(medOrderExec.getIdm());
                medOrderBackupsExec.setDosageForm(medOrderExec.getDosageForm());
                medOrderBackupsExec.setCoefficient(medOrderExec.getCoefficient());
                medOrderBackupsExec.setTreatmentRoute(medOrderExec.getTreatmentRoute());
                medOrderBackupsExec.setStatus(medOrderExec.getStatus());
                medOrderBackupsExecService.insert(medOrderBackupsExec);
            });
            medOrderExecService.deleteByCreateTime(map);
        } catch (Exception e) {
            logger.info("异常：" + e);
        }
    }
}
